package com.minimalist.lindi.mapper;

import com.minimalist.common.entity.lindi.po.LindiTradeApplication;

import com.minimalist.common.entity.lindi.vo.tradeapplication.LindiTradeApplicationQueryVO;
import com.mybatisflex.core.BaseMapper;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;

/**
 * 林地交易指标申请 映射层。
 *
 * @author 11987
 * @since 2025-04-18
 */
public interface LindiTradeApplicationMapper extends BaseMapper<LindiTradeApplication>{

    /**
     * 根据交易申请ID查询交易申请
     *
     * @param tradeApplicationId 交易申请ID
     * @return LindiTradeApplication
     */
    default LindiTradeApplication selectTradeApplicationByTradeApplicationId(
            Long tradeApplicationId){
        return selectOneByQuery(QueryWrapper.create()
                .eq(LindiTradeApplication::getTradeApplicationId
                        , tradeApplicationId)
        );
    }

    /**
     * 分页查询交易申请列表
     *
     * @param queryVO 查询实体
     * @return 分页交易申请列表
     */
    default Page<LindiTradeApplication> selectPageTradeApplicationList(
            LindiTradeApplicationQueryVO queryVO){
        QueryWrapper queryWrapper = QueryWrapper.create();

        // 增加对查询条件空字符串和null的判断
        queryWrapper.eq(LindiTradeApplication::getEnterpriseAccountId
                        , queryVO.getEnterpriseAccountId())
                .eq(LindiTradeApplication::getTradeStatus, queryVO.getTradeStatus())
                .eq(LindiTradeApplication::getProjectType, queryVO.getProjectType())
                .like(LindiTradeApplication::getProjectName, queryVO.getProjectName())
                .orderBy(LindiTradeApplication::getId, false);

        return paginate(queryVO.getPageNum(), queryVO.getPageSize(), queryWrapper);
    }

    /**
     * 根据交易申请ID修改交易申请
     *
     * @param tradeApplication 交易申请
     * @return 修改数量
     */
    default int updateTradeApplicationByTradeApplicationId(LindiTradeApplication tradeApplication){
        return updateByQuery(tradeApplication,
                QueryWrapper.create()
                        .eq(LindiTradeApplication::getTradeApplicationId
                                , tradeApplication.getTradeApplicationId()));
    }
}
